package com.example.project.controller;

import com.example.project.base.BaseApi;
import com.example.project.base.ResultBean;
import com.example.project.entity.TAdmin;
import com.example.project.entity.TRole;
import com.example.project.service.TModuleService;
import com.example.project.service.TRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/trole")
@Api(value = "TRoleController",tags = "管理员角色模块相关")
public class TRoleController extends BaseApi {
    @Autowired
    TRoleService tRoleService;
    @Autowired
    TModuleService tModuleService;

    @RequestMapping(value = "/getAllModuleNoByRoleId",method = RequestMethod.POST)
    @ApiOperation("根据角色获取对应模块")
    public ResultBean getAllModuleNoByRoleId(Integer roleId){
        try{
//            ShiroCacheObj shiroCacheObj=currAdminInfo();
            List<String> moduleSet= tModuleService.getAllModuleNoByRoleId(roleId);
            return ok("成功",moduleSet);
        }catch (Exception e){
            e.printStackTrace();
            return fail("查询失败");
        }
    }
    @RequestMapping(value = "/getAllRole",method = RequestMethod.POST)
    @ApiOperation("获取所有管理员角色")
    public ResultBean getAllRole(Integer pageNo, Integer pageSize){

        try {
            PageHelper.startPage(pageNo==null?0:pageNo,pageSize==null?0:pageSize);
            PageInfo<TRole> list=new PageInfo<>(tRoleService.queryAll());

            return ok(list);
        }catch (Exception e){
            e.printStackTrace();
        }

        return ok();
    }

    @RequestMapping(value = "/giveModuleByRole",method = RequestMethod.POST)
    @ApiOperation("角色赋权")
    public ResultBean giveModuleByRole(@RequestParam Integer roleId,@RequestParam Integer moduleId){
//        if(roleId!=null||roleId>0||moduleId!=null||moduleId>0){
            try {
                tRoleService.giveModuleByRole(roleId,moduleId);
                return ok();
            }catch (Exception e){
                e.printStackTrace();
                return fail();
            }
//        }else{
//            return fail("参数未获取");
//        }

    }

    @RequestMapping(value = "/takeBackModuleByRole",method = RequestMethod.POST)
    @ApiOperation("角色收权")
    public ResultBean takeBackModuleByRole(@RequestParam Integer roleId,@RequestParam Integer moduleId){
//        if(roleId!=null||roleId>0||moduleId!=null||moduleId>0){
        try {
            tRoleService.takeBackModuleByRole(roleId,moduleId);
            return ok();
        }catch (Exception e){
            e.printStackTrace();
            return fail();
        }
//        }else{
//            return fail("参数未获取");
//        }

    }

    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @ApiOperation("添加角色")
    public ResultBean addRole(@RequestParam String roleName){
        try{
            TRole tRole=new TRole(){{
                setRoleName(roleName);
            }};
            tRoleService.addRole(tRole);
            return ok(tRole.getPkid());
        }catch (Exception e){
            e.printStackTrace();
            return fail();
        }
    }

    @RequestMapping(value = "/examineRole",method = RequestMethod.POST)
    @ApiOperation("校验角色名")
    public ResultBean examineRole(@RequestParam String roleName){
        try{
            TRole tRole=new TRole(){{
                setRoleName(roleName);
            }};
            TRole tRole1=tRoleService.queryOne(tRole);
            if(tRole1==null){
                return ok();
            }else{
                return fail();
            }

        }catch (Exception e){
            e.printStackTrace();
            return fail();
        }
    }
    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    @ApiOperation("修改角色名")
    public ResultBean updateRole(@RequestParam String roleName,@RequestParam Integer pkid){
        try{
            Integer roleId=pkid;
            TRole tRole=new TRole(){{
                setPkid(roleId);
                setRoleName(roleName);
            }};
            tRoleService.updateRole(tRole);
            return ok();
        }catch (Exception e){
            e.printStackTrace();
            return fail();
        }
    }
    @RequestMapping(value = "/deleteRole",method = RequestMethod.POST)
    @ApiOperation("删除角色")
    public ResultBean deleteRole(@RequestParam List<Integer> pkids){
        try{
            tRoleService.deleteByIds(TRole.class,pkids);

            return ok();
        }catch (Exception e){
            e.printStackTrace();
            return fail();
        }
    }
}
